package com.example.pufang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.pufang.mapper.ChatMapper;
import com.example.pufang.pojo.Chat;
import com.example.pufang.pojo.User;
import com.example.pufang.service.ChatService;
import com.example.pufang.service.UserService;
import com.example.pufang.vo.ChatListVo;
import com.example.pufang.vo.ChatVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ChatServiceImpl extends ServiceImpl<ChatMapper, Chat> implements ChatService {

    @Autowired
    private ChatMapper chatMapper;

    @Autowired
    private UserService userService;

    @Override
    public List<ChatVo> getList(User sender, User receiver) {

        List<ChatVo> chatVoList = new ArrayList<>();

        LambdaQueryWrapper<Chat> wrapper = new LambdaQueryWrapper<>();
        wrapper.and(w -> w.eq(Chat::getSenderId, sender.getId()).or().eq(Chat::getReceiverId, sender.getId()))
                .and(w -> w.eq(Chat::getSenderId, receiver.getId()).or().eq(Chat::getReceiverId, receiver.getId()))
                .orderByAsc(Chat::getCreateTime);

        for (Chat chat : chatMapper.selectList(wrapper)) {
            ChatVo chatVo = new ChatVo();
            BeanUtils.copyProperties(chat, chatVo);

            if (chatVo.getSenderId() == sender.getId()) {
                chatVo.setSenderLogo(sender.getLogo());
            } else if (chatVo.getSenderId() == receiver.getId()) {
                chatVo.setSenderLogo(receiver.getLogo());
            }
            if (chatVo.getReceiverId() == sender.getId()) {
                chatVo.setReceiverLogo(sender.getLogo());
            } else if (chatVo.getReceiverId() == receiver.getId()) {
                chatVo.setReceiverLogo(receiver.getLogo());
            }

            chatVoList.add(chatVo);
        }

        return chatVoList;
    }

    @Override
    public List<ChatListVo> getPageList(User sender) {
        List<ChatListVo> chatVoList = new ArrayList<>();

        LambdaQueryWrapper<Chat> wrapper = new LambdaQueryWrapper<>();
        wrapper.and(w -> w.eq(Chat::getSenderId, sender.getId()).or().eq(Chat::getReceiverId, sender.getId()))
                .orderByDesc(Chat::getCreateTime);

        for (Chat chat : chatMapper.selectList(wrapper)) {

            for (int i = 0; i <= chatVoList.size(); i++) {
                if (i == chatVoList.size()) {
                    ChatListVo chatListVo = new ChatListVo();
                    // 如果当前用户是发送人，存储接收人信息
                    if (sender.getId() == chat.getSenderId()) {
                        User receiver = userService.getById(chat.getReceiverId());
                        chatListVo.setId(receiver.getId())
                                .setLogo(receiver.getLogo())
                                .setUsername(receiver.getUsername())
                                .setCreateTime(chat.getCreateTime())
                                .setMessage(chat.getMessage());
                    }
                    // 如果当前用户是接收人，存储发送人信息
                    else if (sender.getId() == chat.getReceiverId()) {
                        User receiver = userService.getById(chat.getSenderId());
                        chatListVo.setId(receiver.getId())
                                .setLogo(receiver.getLogo())
                                .setUsername(receiver.getUsername())
                                .setCreateTime(chat.getCreateTime())
                                .setMessage(chat.getMessage());
                    }
                    chatVoList.add(chatListVo);
                    break;
                }
                if (chat.getSenderId() == chatVoList.get(i).getId()) {
                    break;
                } else if (chat.getReceiverId() == chatVoList.get(i).getId()) {
                    break;
                }
            }
        }

        return chatVoList;
    }
}
